package icu.zqyhdm.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.zqyhdm.result.Result;
import icu.zqyhdm.blog.entity.Notice;
import icu.zqyhdm.blog.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description
 * @Author lifu
 * @Date 2021/6/22 21:56
 */
@Api(tags = "网站公告的接口")
@RestController
@RequestMapping(value = "/api/blog/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation(value = "最近的所有公告")
    @GetMapping(value = "/list")
    public Result List() {
        List<Notice> notices = noticeService.list(new QueryWrapper<Notice>().orderByDesc("create_time").last("limit 3"));
        return Result.ok(notices);
    }

    @ApiOperation(value = "增加公告")
    @PostMapping(value = "/add")
    public Result add(@RequestBody Notice notice){
        boolean b = noticeService.save(notice);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation(value = "修改公告")
    @PutMapping(value = "/update")
    public Result update(@RequestBody Notice notice){
        boolean b = noticeService.updateById(notice);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "删除公告")
    @DeleteMapping(value = "/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean b = noticeService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
